<template>
  <div style="position:relative">
    <TopFilter :list="topFilterList" :cols="4" :labelWidth="90" :collapse="false" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>

    <FilterTable
      ref="basetable"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'rows'"
      :isSelectColumn="false"
      :onColumnsChange="columns => (this.columns = columns)"
      columnsRef="zjwxlsrepairhistoryTable"
      :onSyncTableData="maintenanceSyncDataFun"
    ></FilterTable>
    <div style="height:53px"></div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDrawer()">返 回</el-button>
    </div>
    <BaseDialog :visible.sync="settlementVisible" title="结算明细" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <settlement :getdata="settlementdata" @close="closeSpop"></settlement>
    </BaseDialog>
    <BaseDialog :visible.sync="jobVisible" title="维修明细" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <jobdetail :getdata="settlementdata" @close="closeJpop"></jobdetail>
    </BaseDialog>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import settlement from './Detailed/Settlement.vue';
import jobdetail from './Detailed/jobdetail.vue';
import { getmaintainhistoryList, getfindAllRepairType } from '@/api/repair/business/customer/history';
import { getDealerUser } from '@/api/customerRepair/customerComplaint/customerComplaintReception';
import { sleep } from '@/utils';
export default {
  name: 'zjrepairhistory',
  components: {
    settlement,
    jobdetail
  },
  mixins: [dictionary],
  props: ['setdata'],
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createTopFilterList(),
      columns: this.createTable(),
      fetchApi: getmaintainhistoryList,
      fetchParams: { xhrAbort: true, ownerName: this.setdata.OWNER_NAME, license: this.setdata.LICENSE, vin: this.setdata.VIN },
      jobVisible: false,
      settlementVisible: false,
      list: [],
      settlementdata: { RO_NO: '' },
      userList: [] // 翻译人名
    };
  },
  created() {},
  mounted() {
    this.BaseTable = this.$refs.basetable.$refs.pageTable;
    this.getEmp();
    this.getRepairType();
    this.changeHandle();
  },
  methods: {
    async maintenanceSyncDataFun(e) {
      console.log(e, 'table1数据变化');
      if (!Object.keys(this.userList).length) {
        let obj = [];
        const res = await getDealerUser();
        res.data.map(item => {
          obj[item.userId] = item.employeeName;
        });
        this.userList = obj;
      }
      // console.log(123213123123213, this.userList);
      await sleep(600);
      this.$nextTick(() => {
        e.map(item => {
          let arr = item.TECHNICIAN_NAME ? item.TECHNICIAN_NAME.split(',') : [];
          let nameArr = [];
          arr.map(item2 => {
            if (this.userList[item2]) {
              nameArr.push(this.userList[item2]);
            } else {
              nameArr.push(item2);
            }
          });
          item.TECHNICIAN_NAMES_TRANSED = nameArr.join(',');
        });
      });
    },
    createTopFilterList() {
      return [
        {
          type: 'SELECT',
          label: '维修类型',
          fieldName: 'repairTypeCode',
          placeholder: '请选择',
          initialValue: '',
          itemList: []
        },
        {
          type: 'INPUT',
          label: '进厂开始里程',
          fieldName: 'inMileageFrom'
        },
        {
          type: 'INPUT',
          label: '进厂结束里程',
          fieldName: 'inMileageTo'
        }
      ];
    },
    createTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 150,
          render: props => {
            return (
              <div>
                <el-button size="mini" type="text" onClick={() => this.settlementFun(props)} disabled={props.row.RO_STATUS === 80491003 ? false : true}>
                  结算明细
                </el-button>
                <el-divider direction="vertical" />
                <el-button size="mini" type="text" onClick={() => this.jobdetailFun(props)}>
                  维修明细
                </el-button>
              </div>
            );
          }
        },
        {
          title: '工单号',
          dataIndex: 'RO_NO',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        // {
        //   title: '维修项目名称',
        //   dataIndex: 'LABOUR_NAME',
        //   sorter: true,
        //   filter: true
        // },
        {
          title: '车牌号',
          dataIndex: 'LICENSE',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '维修类型',
          dataIndex: 'REPAIR_TYPE_CODE',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: [],
          editType: 'select',
          editable: false,
          editItems: []
        },
        {
          title: '进厂里程',
          dataIndex: 'IN_MILEAGE',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '开单日期',
          dataIndex: 'RO_CREATE_DATE',
          width: 140,
          sorter: true,
          filter: true,
          filterType: 'date-range',
          showOverflowTooltip: true
        },
        {
          title: '服务顾问',
          dataIndex: 'SERVICE_ADVISOR',
          sorter: true,
          filter: true,
          filterType: 'input',
          editType: 'select',
          editable: false,
          editItems: []
        },
        {
          title: '维修技师',
          dataIndex: 'TECHNICIAN_NAMES_TRANSED',
          // dataIndex: 'TECHNICIAN_NAME',
          sorter: true,
          filter: true,
          filterType: 'input',
          editType: 'select',
          editable: false,
          editItems: []
        },
        {
          title: '结算日期',
          dataIndex: 'RO_SETTLE_DATE',
          width: 140,
          sorter: true,
          filter: true,
          filterType: 'date-range',
          showOverflowTooltip: true
        },
        {
          title: '工单状态',
          dataIndex: 'RO_STATUS',
          sorter: true,
          filter: true,
          filterType: 'radio',
          filterItems: this.createDictList(8049),
          render: props => {
            return <span>{this.createDictText(props.row.RO_STATUS, '8049')}</span>;
          }
        }
      ];
    },
    // 搜索
    changeHandle(val) {
      if (!this.setdata.VIN) {
        this.$notify.warning({ title: '提示信息', message: '请输入VIN' });
      } else {
        this.fetchParams = Object.assign({}, val, {
          xhrAbort: false,
          ownerName: this.setdata.OWNER_NAME,
          license: this.setdata.LICENSE,
          vin: this.setdata.VIN,
          FH_FLAG: this.setdata.FH_FLAG === 'true' ? this.setdata.FH_FLAG : null // 售后回访调用维修历史时会传this.setdata.FH_FLAG = 'true',根据这个参数查询所有状态结算单的维修历史。其他页面传null
        });
      }
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    closeDrawer() {
      this.$emit('close', false);
    },
    // 打开结算明细
    settlementFun(data) {
      this.settlementdata = { RO_NO: data.row.RO_NO };
      this.settlementVisible = true;
    },
    // 打开维修明细
    jobdetailFun(data) {
      this.settlementdata = { RO_NO: data.row.RO_NO };
      this.jobVisible = true;
    },
    closeSpop(val) {
      this.settlementVisible = false;
    },
    closeJpop(val) {
      this.jobVisible = false;
    },
    // 获取全部员工
    async getEmp() {
      const res = await getDealerUser();
      let empList = res.data.map(x => ({ text: x.employeeName, value: x.userId + '' }));
      this.columns.find(item => item.dataIndex === 'SERVICE_ADVISOR').editItems = empList; // 服务专员
      // this.columns.find(item => item.dataIndex === 'TECHNICIAN_NAME').editItems = empList; // 服务专员 hxj 前端报错 去除 翻译已由其他地方处理
      res.data.map(x => (this.userList[x.userId] = x.employeeName));
      console.log(this.userList, '人员列表');

      let res1 = res.data.map(x => ({ text: x.employeeName, value: x.userId + '' }));
      res1.push({ text: '合派', value: '合派' });
      window.console.log(res1);
    },
    // 获取维修类型
    async getRepairType() {
      const res = await getfindAllRepairType();
      let list = res.data.map(x => ({ text: x.REPAIR_TYPE_NAME, value: x.REPAIR_TYPE_CODE + '' }));

      this.topFilterList.find(item => item.fieldName === 'repairTypeCode').itemList = list; // 维修类型

      this.columns.find(item => item.dataIndex === 'REPAIR_TYPE_CODE').editItems = this.columns.find(item => item.dataIndex === 'REPAIR_TYPE_CODE').filterItems = list; // 维修类型表格
    }
  }
};
</script>
